package com.ithema.b2b.goodsconsumer.controller;

import com.ithema.b2b.goodsconsumer.entity.PayGoodsParam;
import com.ithema.b2b.goodsconsumer.service.GoodsService;
import com.netflix.ribbon.proxy.annotation.Http;
import model.Cart;
import model.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @GetMapping(value = "/getAll")
    public String getAll(HttpServletRequest request,
                         String uaccount,
                         String upassword,
                         String uname,
                         String usex,
                         HttpSession session) {
        session.setAttribute("uaccount", uaccount);
        List<Goods> oldgoods = goodsService.getAllgoods();
        if (oldgoods.isEmpty()) {
            System.out.println("查询结果集为空！请确认输入参数！");
            return "查询结果集为空！请确认输入参数！";
        }

        setAttributes(request, oldgoods);
        return "index";
    }

    @GetMapping(value = "getOne")
    public String getOne(HttpServletRequest request,
                         String gname) {
        List<Goods> oldGoods = goodsService.getOnegoods(gname);
        if (oldGoods.isEmpty()) {
            System.out.println("查询结果集为空！请确认输入参数！");
            return "查询结果集为空！请确认输入参数！";
        }

        setAttributes(request, oldGoods);
        return "index";
    }

    @GetMapping(value = "/detail")
    public String detail(HttpServletRequest request, int gid) {
        request.setAttribute("detail", goodsService.getOnegid(gid));
        return "detail";
    }

    private void setAttributes(HttpServletRequest request,
                               List<Goods> oldGoodsList) {
        if (oldGoodsList.isEmpty()) {
            System.out.println("OldGoodsListIsEmpty!!!");
            return;
        }

        List<Goods> goods0 = new ArrayList<>();
        List<Goods> goods1 = new ArrayList<>();
        List<Goods> goods2 = new ArrayList<>();
        List<Goods> goods3 = new ArrayList<>();

        for (Goods oldGoods : oldGoodsList) {
            System.out.println(oldGoods.getTypes());
            if (oldGoods.getTypes().equals(0)) {
                goods0.add(oldGoods);
            } else if (oldGoods.getTypes().equals(1)) {
                goods1.add(oldGoods);
            } else if (oldGoods.getTypes().equals(2)) {
                goods2.add(oldGoods);
            } else if (oldGoods.getTypes().equals(3)) {
                goods3.add(oldGoods);
            } else {
                System.out.println("未识别的商品类型！");
                continue;
            }
        }

        request.setAttribute("goods0", goods0);
        request.setAttribute("goods1", goods1);
        request.setAttribute("goods2", goods2);
        request.setAttribute("goods3", goods3);
    }

    @GetMapping(value = "/cart")
    public String insertCart(HttpServletRequest request,
                             String name,
                             String price,
                             String goodId,
                             String number,
                             HttpSession session) {
        if (session.getAttribute("uaccount") == null) {
            System.out.println("UAccountWasNotFound!");
            return "redirect:/goods/cartAll";
        }

        String uid = (String) session.getAttribute("uaccount");
        List<Cart> carts = goodsService.getAllCart(Integer.parseInt(uid));
        if (carts != null && !carts.isEmpty()) {
            for (Cart newCart : carts) {
                if (newCart.getGoodid() == Integer.parseInt(goodId)) {
                    goodsService.updateCart(Integer.parseInt(number), newCart.getId());
                    return "redirect:/goods/cartAll";
                }
            }
        }

        goodsService.insertCart(name,
                Integer.parseInt(number),
                Integer.parseInt(price),
                Integer.parseInt(goodId),
                Integer.parseInt(uid));
        return "redirect:/goods/cartAll";
    }

    @GetMapping(value = "/cartAll")
    public String cartAll(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("uaccount") == null) {
            System.out.println("UAccountWasNotFound!");
            return "redirect:/goods/cartAll";
        }

        List<Cart> carts = goodsService.getAllCart(Integer.parseInt((String)session.getAttribute("uaccount")));
        request.setAttribute("carts", carts);
        return "Settle";
    }

    @GetMapping(value = "/deleteCart")
    public String deleteCart(HttpServletRequest request, String did) {
        int result = goodsService.deleteCart(Integer.parseInt(did));
        // 根据delete返回结果判断是否删除成功
        // 对于insert，update等，用executeUpdate执行后如返回的值是0则失败，为正整数则成功
        if (result == 0) {
            System.out.println("NoCartDeleted!");
        }

        return "redirect:/goods/cartAll";
    }

    @GetMapping(value = "/paygoods")
    public String paygoods(HttpServletRequest request,
                           HttpSession session,
                           String[] goodsids,
                           String[] numbers,
                           String[] cartids) {
        if (goodsids.length == 0
                || numbers.length == 0
                || cartids.length == 0) {
            System.out.println("参数异常！");
            return "redirect:/goods/cartAll";
        } else {
            int goodsNumber = 0;
            Goods goods = null;

            if (session.getAttribute("uaccount") == null) {
                System.out.println("UAccountWasNotFound!");
                return "redirect:/goods/cartAll";
            }

            String uid = (String)session.getAttribute("uaccount");
            for (int idx = 0; idx < goodsids.length; idx++) {
                goodsNumber = Integer.parseInt(numbers[idx]);
                goods = goodsService.getOnegid(Integer.parseInt(goodsids[idx]));
                goodsService.insertOrder(goods.getGname(),
                        goodsNumber,
                        goodsNumber * goods.getGprice(),
                        Integer.parseInt(uid));
                goodsService.deleteCart(Integer.parseInt(cartids[idx]));
            }
//            for (PayGoodsParam payGoodsParam : payGoodsParams) {
//                goodsNumber = Integer.parseInt(payGoodsParam.getGoodsNumber())
//                goods = goodsService.getOnegid(Integer.parseInt(payGoodsParam.getGoodsId()));
//                goodsService.insertOrder(goods.getGname(),
//                        goodsNumber,
//                        goodsNumber * goods.getGprice(),
//                        Integer.parseInt(uid));
//                goodsService.deleteCart(Integer.parseInt(cartids[idx]));
//            }

            return "redirect:/goods/getAllorder";
        }
    }

    @GetMapping(value = "/getAllorder")
    public String getAllorder(HttpServletRequest request,
                              HttpSession session) {
        // 当前用户未登录，强制跳转登录页面
        if (session.getAttribute("uaccount") == null) {
            System.out.println("UAccountWasNotFound!");
            return "redirect:/admin/tologin";
        }

        String uid = (String)session.getAttribute("uaccount");
        request.setAttribute("order", goodsService.getAllorder(Integer.parseInt(uid)));

        return "order";
    }
}
